package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.Payable;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.StockInProduct;
import com.fowo.api.model.payable.PayableVo;
import com.fowo.api.model.payable.product.PayableProductItemVo;
import com.fowo.api.model.stock.in.StockInPushPayableAction;
import com.fowo.api.service.PayableProductService;
import com.fowo.api.service.PayableService;
import com.fowo.api.service.impl.PayableProductServiceImpl;
import com.fowo.api.service.impl.PayableServiceImpl;
import com.fowo.api.service.impl.StockInProductServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import liquibase.pro.packaged.E;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class stockInPushPayable implements StockInPushPayableAction {

    @Resource
    private StockInServiceImpl stockInService;
    @Resource
    private StockInProductServiceImpl stockInProductService;
    @Resource
    private PayableServiceImpl payableService;
    @Resource
    private PayableProductServiceImpl payableProductService;



    @Override
    public void run(Long id) throws Exception {
        StockIn stockIn = stockInService.getById(id);
        Assert.notNull(stockIn,"未查到入库单!");
       Assert.notNull(stockIn.getGenerateAp(),"未审核!");
       Assert.isTrue(stockIn.getGenerateAp().equals("0"),"已生成不能重复生成!");
       Assert.isTrue(stockIn.getStatus().equals("1"),"入库单未审核!");//
        List<StockInProduct> list = stockInProductService.list(Wrappers.<StockInProduct>query().lambda().eq(StockInProduct::getParentStockInId, id));
    /*    List<Payable> list1 = payableService.list(Wrappers.<Payable>query().lambda().eq(Payable::getRemarks, stockIn.getRemarks()+",入库单号:"+stockIn.getOrderSn()));
        Assert.isTrue(list1.size()==0,"已下推应付单,不能重复下推!");*/

        addPayable(stockIn,list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addPayable(StockIn stockIn,List<StockInProduct> list) {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        PayableVo model =new PayableVo();
        model.setStauts("0");
        model.setPayOrg(stockIn.getApplicationDep());
        model.setPurchasesOrg(stockIn.getPurchasingOrg());
        model.setSettlementOrg(stockIn.getNeedOrg());
        //截取入库单号
        String po=stockIn.getRemarks();
        String remarks = stockIn.getRemarks() == null ? "" : stockIn.getRemarks()+",";
        model.setRemarks(remarks+"入库单号:"+stockIn.getOrderSn());
        model.setBusinessDate(stockIn.getWarehousingDate());
        model.setSupplierId(stockIn.getSupplier());
        model.setProductType(stockIn.getProductType());
        model.setPushStatus("0");
        model.setWarehouse(stockIn.getWarehouse());
        model.setSettlementDate(stockIn.getWarehousingDate());
        model.setPreparedBy(currentUser.getUserId());
        model.setPurchaseOrder(getOrder(stockIn.getRemarks())==null?stockIn.getPurchaseOrderCode():getOrder(stockIn.getRemarks()));
        model.setWarehouseSn(stockIn.getOrderSn());
        List<PayableProductItemVo> productltem=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            StockInProduct stockInProduct = list.get(i);
            PayableProductItemVo payableProductItemVo=new PayableProductItemVo();
            payableProductItemVo.setNum(stockInProduct.getQuantity());
            payableProductItemVo.setPrice(stockInProduct.getUnitPrice());
            payableProductItemVo.setGramWeight(stockInProduct.getGramWeight());
            payableProductItemVo.setMainUnitPrice(stockInProduct.getMainUnitPrice());
            payableProductItemVo.setLaborCosts(stockInProduct.getLaborCosts());
            payableProductItemVo.setName(stockInProduct.getProductName());
            payableProductItemVo.setProductId(stockInProduct.getProductId());
            payableProductItemVo.setSku(stockInProduct.getSku());
            payableProductItemVo.setSubtotal(stockInProduct.getSubtotal());
            productltem.add(payableProductItemVo);
        }
        model.setProductltem(productltem);
        try {
            payableService.create(model);
        } catch (Exception e) {
            throw new RuntimeException("");
        }
        stockIn.setGenerateAp("1");
        stockIn.setRevocation("0");
        stockInService.updateById(stockIn);
    }


    String getOrder(String orderSn){
      String po=null;
      if(orderSn!=null){
          if(orderSn.indexOf("PO")!=-1){
              try{
                  po=orderSn.substring(orderSn.indexOf("PO"),orderSn.indexOf("PO")+11);
              }catch (Exception e){

              }
          }
      }
        return  po;
    }

}
